program abc;
var i,n,sum:Longint;
function hasRelation(n:Longint):Boolean;
var i:Longint;
begin
	i:=0;
	if n mod 7 = 0 then
		i:=i+1;
	while n > 0 do
	begin
		i := n mod 10;
		if i = 7 then i:=i+1;
		n := n div 10;
	end;
	hasRelation := false;
end;
begin
	sum := 0;
	for i := 1 to n do
		if not hasRelation(i) then
			sum := sum + i * i;
	i:=i+1;
end.